<?php namespace Model;
    class userModel {
        private $DB;
        protected $PHONE;
        protected $EMAIL;
        protected $ID;

        public function __construct() {
            $this->DB = mysqli_connect('kvd_mysql', 'root', 'root', 'kvd');
        }

        public function addUser($phone, $email = null) {
            $regexp = '/^\s?(\+\s?7|8)([- ()]*\d){10}$/';
            $data = date("Y.m.d");
            $sql = "";

            if (!(preg_match($regexp, $phone)) && !preg_replace("/[^0-9]/", '', $phone)) {
                return [
                    "status" => 0
                ];
            }
            if ($phone[0] == "8") {
                $phone[0] = "7";
            }
            elseif ($phone[0] == "+") {
                str_replace("+", "", $phone);
            }

            $sql = "select * from users where phone = $phone";
            $response = $this->DB->query($sql)->fetch_assoc();
            $token = md5($phone);


            if ($response !== null) {
                return [
                    "response" => $response["token"],
                    "phone" => $phone,
                    "status" => "update"
                ];
            } else {
                $sql = "INSERT INTO users (phone, email, created, updated, token)
                            VALUES
                            ('$phone', '$email', '$data', '$data', '$token')";
                $this->DB->query($sql);
                $response = $this->DB->query("select * from users where phone = $phone")->fetch_assoc();
                if ($response != null) {
                    return [
                        "response" => $response["token"],
                        "phone" => $phone,
                        "status" => "create"
                    ];
                } else {
                    return [
                        "response" => "Not found"
                    ];
                }
            }
        }

        public function getUserBy($id = null) {
            $id = (integer) $id;
            $sql = "select * from `users` where id = $id";
            $response = $this->DB->query($sql)->fetch_assoc();
            if ($response === null) {
                return [
                    "status" => 0
                ];
            } else {
                return [
                    "status" => 1,
                    "response" => $response
                ];
            }
        }

        public function getAllUsers() {
            $sql = "SELECT * FROM users";
            $sql = $this->DB->real_escape_string($sql);
            $response = $this->DB->query($sql)->fetch_all(MYSQLI_ASSOC);
            if ($response !== null) {
                return [
                    "response" => $response,
                    "status" => 1
                ];
            } else {
                return [
                    "status" => 0
                ];
            }
        }

        public function checkUserToken($token) {
            $sql = "select * from users where token = '$token'";
            $response = $this->DB->query($sql)->fetch_assoc();
            if ($response != null) {
                return [
                    "status" => 1,
                    "response" => $response
                ];
            } else {
                return [
                    "status" => 0
                ];
            }
        }

        public function getAllUserOrders($token) {
            $sql = "select * from kvd.users where token = '$token'";
            $user_id = $this->DB->query($sql)->fetch_assoc()['id'];

            $sql = "select * from kvd.orders where user_id = $user_id";
            $response = $this->DB->query($sql)->fetch_all(MYSQLI_ASSOC);

            if ($response == null){
                return [
                    "status" => 0
                ];
            } else {
                return [
                    "status" => 1,
                    "response" => $response
                ];
            }
        }

        public function getAllUserEvents($token) {
            $sql = "select * from kvd.users where token = '$token'";
            $user_id = $this->DB->query($sql)->fetch_assoc()['id'];

            $sql = "select * from kvd.orders where user_id = $user_id";
            $response = $this->DB->query($sql)->fetch_all(MYSQLI_ASSOC);
            $user_events = [];


            foreach ($response as $key => $item){
                $id = $item['event_id'];
                $sql = "select * from kvd.events where id = $id";
                $result = $this->DB->query($sql)->fetch_assoc();
                array_push($user_events, $result);

            }

            if ($response === null){
                return [
                    "status" => 0
                ];
            } else {
                return [
                    "status" => 1,
                    "response" => $user_events
                ];
            }
        }

        public function editUser($id, $phone, $age, $name, $email) {
            $id = (integer) $id;
            $age = (integer) $age;
            $data = date("Y.m.d");
            $regexp = '/^\s?(\+\s?7|8)([- ()]*\d){10}$/';
            $phone = (string) $phone;
            if (!(preg_match($regexp, $phone)) && !preg_replace("/[^0-9]/", '', $phone)) {
                return [
                    "status" => 0
                ];
            }
            if ($phone[0] == "8") {
                $phone[0] = "7";
            }
            elseif ($phone[0] == "+") {
                str_replace("+", "", $phone);
            }
            $sql = "update users set phone = '$phone', age = $age, name = '$name', email = '$email', updated = '$data' where id = $id";
            $this->DB->query($sql);

            $sql = "select * from users where id = $id";
            $response = $this->DB->query($sql)->fetch_assoc();
            if ($response == null) {
                return [
                    "status" => 0
                ];
            } else {
                return [
                    "response" => $response,
                    "status" => 1
                ];
            }
        }

        public function deleteUser($admin_id, $user_id) {
//            $sql = "select * from kvd.events where admin_id = $admin_id and id = $event_id";
//            $response = $this->DB->query($sql)->fetch_assoc();
//            if ($response == null){
//                return [
//                    "status" => 0
//                ];
//            }
//            $sql = "delete from events where admin_id = $admin_id and id = $event_id";
//            $response = $this->DB->query($sql);
//            $sql = "select * from kvd.events where admin_id = $admin_id and id = $event_id";
//            $response = $this->DB->query($sql)->fetch_assoc();
//            if ($response == null){
//                return [
//                    "status" => 1
//                ];
//            } else {
//                return [
//                    "status" => 0
//                ];
//            }
        }

    }